package com.dr.dac.knowledgebase.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.dr.dac.knowledgebase.ui.bean.KFunction;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by weifeiyang on 2016/12/26.
 */

public class KFunctionDataBase {
    private final DatabaseHelper dbHelper;

    public KFunctionDataBase(Context context) {
        super();
        dbHelper = DatabaseHelper.getInstance(context);

        //dropTable();
        //createTable();
    }

    /**
     * 增
     *
     * @param data
     */
    public void insert(KFunction data) {
        String sql = "replace into " + DatabaseHelper.TABLE_NAME_K_FunctionPoint;

        sql += "(FPointID,ClassSID,FPointIDName,HandleID,KeyWord)"+ "values(?,?,?,?,?)";

        SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
        sqlite.execSQL(sql, new String[]{data.getFPointID(),data.getClassSID(),
                data.getFPointIDName(),data.getHandleID(),data.getKeyWord()});
        sqlite.close();
    }

    /**
     * 删
     *
     * @param FPointID
     */
    public void delete(String FPointID) {
        SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
        String sql = ("delete from " + DatabaseHelper.TABLE_NAME_K_FunctionPoint + " where FPointID=?");
        sqlite.execSQL(sql, new String[]{FPointID});
        sqlite.close();
    }

    /**
     * 改
     *
     * @param data
     */
    public void updateAvatar(KFunction data) {
//        SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
//        String sql = ("update " + DatabaseHelper.TABLE_NAME_K_CLASS + " set avatar=? where userId=?");
//        sqlite.execSQL(sql, new String[]{data.getAvatar(), data.getUserId()});
//        sqlite.close();
    }


    public List<KFunction> query() {
        return query(" ");
    }

    /**
     * 查
     *
     * @param where
     * @return
     */
    public List<KFunction> query(String where) {
        SQLiteDatabase sqlite = dbHelper.getReadableDatabase();
        ArrayList<KFunction> data = null;
        data = new ArrayList<KFunction>();
        Cursor cursor = sqlite.rawQuery("select * from "
                + DatabaseHelper.TABLE_NAME_K_FunctionPoint + where, null);
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            KFunction info = new KFunction();
            info.setFPointID(cursor.getString(0));
            info.setClassSID(cursor.getString(1));
            info.setFPointIDName(cursor.getString(2));
            info.setHandleID(cursor.getString(3));
            info.setKeyWord(cursor.getString(4));
            data.add(info);
        }

        if (!cursor.isClosed()) {
            cursor.close();
        }
        sqlite.close();

        return data;
    }

    /**
     * 查
     *
     * @return
     */
    public String queryKey(String HandleID) {
        SQLiteDatabase sqlite = dbHelper.getReadableDatabase();
        KFunction info = new KFunction();
        info.setKeyWord("");
        Cursor cursor = sqlite.rawQuery("select * from "
                + DatabaseHelper.TABLE_NAME_K_FunctionPoint + " where HandleID = "+"'"+HandleID+"'", null);
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            info.setFPointID(cursor.getString(0));
            info.setClassSID(cursor.getString(1));
            info.setFPointIDName(cursor.getString(2));
            info.setHandleID(cursor.getString(3));
            info.setKeyWord(cursor.getString(4));
            break;
        }

        if (!cursor.isClosed()) {
            cursor.close();
        }
        sqlite.close();

        return info.getKeyWord();
    }


    /**
     * 重置
     *
     * @param data
     */
    public void reset(KFunction data) {
        if (data != null) {
            SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
            // 删除全部
            sqlite.execSQL("delete from " + DatabaseHelper.TABLE_NAME_K_FunctionPoint);
            // 重新添加
            insert(data);

            sqlite.close();
        }
    }

    /**
     * 保存数据到本地 (若已存在则直接覆盖)
     *
     * @param data
     */
    public void save(KFunction data) {
        List<KFunction> datas = query(" where FPointID = " + data.getFPointID());
        if (datas != null && !datas.isEmpty()) {
            delete(data.getFPointID());
            insert(data);
        } else {
            insert(data);
        }
    }


    public void destroy() {
        dbHelper.close();
    }

    /**
     * 删除表
     */
    public void dropTable() {

        SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
        sqlite.execSQL("DROP TABLE IF EXISTS " + DatabaseHelper.TABLE_NAME_K_FunctionPoint);
        sqlite.close();
    }

    /**
     * 新建表
     */
    public void createTable() {
        SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
        sqlite.execSQL(DatabaseHelper.CREATE_TABLE_K_FunctionPoint);
        sqlite.close();
    }
}
